﻿using System;
using System.Text;
using RabbitMQ.Client;

namespace Producer
{
    class Producer
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    //声明一个fanout类型的交换器
                    //channel.ExchangeDeclare("logs", "fanout");
                    //channel.ExchangeDeclare("direct_logs", ExchangeType.Direct);
                    channel.ExchangeDeclare("topic_logs", ExchangeType.Topic);

                    //var severity = (args.Length > 0) ? args[0] : "info";
                    var severity = (args.Length > 0) ? args[0] : "anonymous.info";
                    string message = GetMessage(args);

                    var body = Encoding.UTF8.GetBytes(message);

                    //channel.BasicPublish(exchange: "direct_logs", routingKey: severity, null, body: body);
                    channel.BasicPublish(exchange: "topic_logs", routingKey: severity, null, body: body);
                    Console.WriteLine("[x] Send {0}", message);

                    Console.WriteLine("Press [enter] to exit.");
                    Console.ReadLine();
                }


            }
        }
        private static string GetMessage(string[] args)
        {
            return ((args.Length) > 0 ? string.Join(" ", args) : "Hello World");
        }
    }
}
